import org.apache.spark.graphx.{Edge, EdgeTriplet, Graph}
import org.apache.spark.{SparkConf, SparkContext}

object Q2_SparkGraphXexercise {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName(this.getClass.getCanonicalName.init).setMaster("local[*]")
    val sc = new SparkContext(conf)
    sc.setLogLevel("WARN")

    val vertexArray = Array(
      (1L, "SFO"),
      (2L, "ORD"),
      (3L, "DFW")
    )
    val EdgeArray = Array(
      Edge(1L, 2L, 1800),
      Edge(2L, 3L, 800),
      Edge(3L, 1L, 1400)
    )
    //构造RDD
    val vertexRDD = sc.makeRDD(vertexArray)
    val edgeRDD = sc.makeRDD(EdgeArray)
    //定义图
    val graph = Graph(vertexRDD, edgeRDD)
    println("求所有的顶点")
    graph.vertices.foreach(println)
    println("求所有的边")
    graph.edges.foreach(println)
    println("求所有的triplets")
    graph.triplets.foreach(println)
    println("求顶点数")
    println(graph.numVertices)
    println("求边数")
    println(graph.numEdges)
    println("求机场距离大于1000的有几个，有哪些按所有机场之间的距离排序（降序），输出结果")
    val num = graph.triplets.filter(t => t.attr > 1000).count()
    println(s"机场距离大于1000的有${num}个")
    graph.triplets.filter(t=> t.attr>1000).sortBy(_.attr,ascending = false).foreach(println)


    sc.stop()
  }
}
